﻿using System;
using System.Collections.Generic;
using System.Linq;
using BSU.CSCE.Data.Filters;
using BSU.CSCE.Data.Model;
using BSU.CSCE.Data.Repositories.Interfaces;

namespace BSU.CSCE.Data.Repositories
{
    public class UserRepository : BaseRepository<User, int>, IUserRepository
    {
        #region IUserRepository Members

        public override User Get(int id)
        {
            CSCEDataContext context = GetNewContext();
            return base.Get(id, context);
        }

        public override User Save(User entity)
        {
            CSCEDataContext context = GetNewContext();
            return base.Save(entity, context);
        }

        public virtual List<User> List(string query, string sortPath, bool asceding, int startIndex, int count, out int totalCount)
        {
            
            CSCEDataContext context = GetNewContext();

            IQueryable<User> users = context.Users;
            if (!String.IsNullOrEmpty(query))
                users =
                    users.Where(
                        u =>
                        u.FirstName.Contains(query) ||
                        u.MiddleName.Contains(query) ||
                        u.LastName.Contains(query));

            return users
                .Sort(sortPath, asceding)
                .Page(startIndex, count, out totalCount).ToList();
        }

        #endregion
    }
}
